package org.snare.alumni.modules.admin.student.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.snare.alumni.modules.admin.student.dto.StudentDTO;
import org.snare.alumni.modules.admin.student.entity.StudentEntity;
import org.snare.alumni.modules.admin.student.mapper.StudentMapper;
import org.snare.alumni.modules.admin.student.service.api.StudentService;
import org.snare.spider.cobweb.common.util.ValidGroup;
import org.snare.spider.cobweb.common.util.ValidatorUtils;
import org.snare.spider.common.base.Limit;
import org.snare.spider.common.exception.BusinessException;
import org.springframework.stereotype.Service;


@Service
@RequiredArgsConstructor
public class StudentServiceImpl implements StudentService {

    private final StudentMapper studentMapper;

    @Override
    public IPage<StudentEntity> page(StudentDTO params, Limit limit) {
        return new LambdaQueryChainWrapper<>(studentMapper)
                .like(StringUtils.isNotBlank(params.getName()), StudentEntity::getName, params.getName())
                .eq(StringUtils.isNotBlank(params.getStatus()), StudentEntity::getStatus, params.getStatus())
                .orderByDesc(StudentEntity::getStatus, StudentEntity::getCreateTime)
                .page(limit.page());
    }

    @Override
    public void add(StudentEntity params) {
        ValidatorUtils.validation(params, new Class[]{ValidGroup.Insert.class});
        studentMapper.insert(params);
    }

    @Override
    public void edit(StudentEntity params) {
        ValidatorUtils.validation(params, new Class[]{ValidGroup.Update.class});
        studentMapper.updateById(params);
    }

    @Override
    public void delete(String id) {
        studentMapper.deleteById(id);
    }

    @Override
    public StudentEntity detail(String id, String uid) {
        if (StringUtils.isBlank(id) && StringUtils.isBlank(uid)) {
            throw new BusinessException("参数错误");
        }
        return new LambdaQueryChainWrapper<>(studentMapper)
                .eq(StringUtils.isNotBlank(id), StudentEntity::getId, id)
                .eq(StringUtils.isNotBlank(uid), StudentEntity::getUid, uid)
                .one();
    }


}
